package com.yueya.api.knowledge;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.business.knowledge.model.Knowledge;
import com.yueya.business.knowledge.model.KnowledgeText;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

import net.sf.json.JSONObject;

public class IKnowledgeService extends BaseService {
	
	//根据知识分类查询知识
	public Resp getPageKnowledge(JSONObject jo) {
		
	    if (containsKey(jo, "pgn", "pgs")) {
			pageNumber = jo.getInt("pgn");
			pageSize = jo.getInt("pgs");
			
			String select = "select k.title,k.summary,d.dict_name as knowledge_type_name,k.create_time ";
			StringBuffer sql = new StringBuffer(" from  jg_knowledge k left join t_dict d on d.dict_value = k.knowledge_type and d.category = 'knowledge_type'  where k.del_flag='0' ");
			
			//查询条件
			if(jo.containsKey("knowledgeType")) {
				String knowledgeType = jo.getString("knowledgeType");
				 sql.append(" and d.dict_value ='"+knowledgeType+"'");					
			}	
			
			Page<Record> page = Db.paginate(pageNumber, pageSize, select.toString(), sql.toString());
			if(null != page) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("查询成功");
				resp.setData(page);
			}else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("查询的数据为空");;
			}
			
	    } else {
			resp.setCode( CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}
	
	/**
	 * 新增知识
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp addKnowledge(JSONObject jo) {
		
		if (containsKey(jo, "title", "knowledgeType", "knowledgeText", "knowledgeHtml")) {
			Knowledge knowledge = Duang.duang(Knowledge.class);
			KnowledgeText knowledgeText = Duang.duang(KnowledgeText.class);
			String title = jo.getString("title");
			knowledge.set("title", title);// 标题
			String knowledgeType = jo.getString("knowledgeType");
			knowledge.set("knowledge_type", knowledgeType);//类型
			String text = jo.getString("knowledgeText");
			knowledgeText.set("knowledge_text", text);//内容
			String html = jo.getString("knowledgeHtml");
			knowledgeText.set("knowledge_html", html);//内容html
	
			// 摘要
			if (jo.containsKey("summary")) {
				String summary = jo.getString("summary");
				knowledge.set("summary", summary);
			}
	
			// 排序值
			if (jo.containsKey("orderNum")) {
				//手填
				String orderNum = jo.getString("orderNum");
				knowledge.set("order_num", orderNum);
			}else {
				//查询获取
				Integer ordernum = Knowledge.dao.getOrdernum();
				knowledge.set("order_num", ordernum);
			}
			
			// 置顶
			if (jo.containsKey("isTop")) {
				String isTop = jo.getString("isTop");
				knowledge.set("is_top", isTop);
			}
			knowledge.set("knowledge_id", UUIDGenerator.getUUID());
			knowledge.set("create_time", DateUtil.getDate(DateUtil.timeFormat));
			
			if(knowledge.save()) {
				knowledgeText.set("knowledge_text_id", UUIDGenerator.getUUID());
				knowledgeText.set("knowledge_id", knowledge.get("knowledge_id"));
				knowledgeText.set("create_time", DateUtil.getDate(DateUtil.timeFormat));
				if(knowledgeText.save()) {
					resp.setCode(CodeConst.SUCCESS);
					resp.setMsg("添加成功");
				}else {
					resp.setCode(CodeConst.KONWLEDGE_TEXT_SAVE_FAIL);
					resp.setMsg("知识文本添加失败");
				}
				
			}else {
				resp.setCode(CodeConst.KONWLEDGE_SAVE_FAIL);
				resp.setMsg("知识添加失败");
			}
			
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		 
		return resp;
	}
	
	/**
	 * 更新知识
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp updateKnowledge(JSONObject jo) {
		if (containsKey(jo, "knowledgeId")) {
			String knowledgeId = jo.getString("knowledgeId");
			Knowledge knowledge = Knowledge.dao.findById(knowledgeId);
			if(null != knowledge) {
				KnowledgeText knowledgeText = KnowledgeText.dao.findByKnowledgeId(knowledgeId);
				if(null != knowledgeText) {
					// 标题
					if (jo.containsKey("title")) {
						String title = jo.getString("title");
						knowledge.set("title", title);
					}
					// 知识类型
					if (jo.containsKey("knowledgeType")) {
						String knowledgeType = jo.getString("knowledgeType");
						knowledge.set("knowledge_type", knowledgeType);
					}
					// 摘要
					if (jo.containsKey("summary")) {
						String summary = jo.getString("summary");
						knowledge.set("summary", summary);
					}
					// 排序值
					if (jo.containsKey("orderNum")) {
						String orderNum = jo.getString("orderNum");
						knowledge.set("order_num", orderNum);
					}
					
					// 置顶
					if (jo.containsKey("isTop")) {
						String isTop = jo.getString("isTop");
						knowledge.set("is_top", isTop);
					}
					knowledge.set("update_time", DateUtil.getDate(DateUtil.timeFormat));
					if(knowledge.update()) {
						
						// 置顶
						if (jo.containsKey("knowledgeText")) {
							String text = jo.getString("knowledgeText");
							knowledgeText.set("knowledge_text", text);
						}
						// 置顶
						if (jo.containsKey("knowledgeHtml")) {
							String html = jo.getString("knowledgeHtml");
							knowledgeText.set("knowledge_html", html);
						}
						knowledgeText.set("update_time", DateUtil.getDate(DateUtil.timeFormat));
						if(knowledgeText.update()) {
							resp.setCode(CodeConst.SUCCESS);
							resp.setMsg("更新成功");
						}else {
							resp.setCode(CodeConst.KONWLEDGE_TEXT_UPDATE_FAIL);
							resp.setMsg("知识文本更新失败");
						}
					}else {
						resp.setCode(CodeConst.KONWLEDGE_UPDATE_FAIL);
						resp.setMsg("知识更新失败");
					}
				}else {
					resp.setCode(CodeConst.KNOWLEDGE_TEXT_NOT_FOUND);
					resp.setMsg("更新的知识文本不存在");
				}
			}else {
				resp.setCode(CodeConst.KNOWLEDGE_NOT_FOUND);
				resp.setMsg("更新的知识不存在");
			}
		}else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}
	
	/**
	 * 更新知识
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp deleteKnowledge(JSONObject jo) {
		if (containsKey(jo, "knowledgeId")) {
			String knowledgeId = jo.getString("knowledgeId");
			Knowledge knowledge = Knowledge.dao.findById(knowledgeId);
			if(null != knowledge) {
				knowledge.set("del_flag", "1");
				knowledge.set("update_time", DateUtil.getDate(DateUtil.timeFormat));
				if(knowledge.update()) {
					resp.setCode(CodeConst.SUCCESS);
					resp.setMsg("删除成功");
				}else {
					resp.setCode(CodeConst.KNOWLEDGE_DELETE_FAIL);
					resp.setMsg("知识删除失败");
				}
			}else {
				resp.setCode(CodeConst.KNOWLEDGE_NOT_FOUND);
				resp.setMsg("删除的知识未找到");
			}
			
		}else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}

	/**
	 * 查看某一知识
	 * @param jo
	 * @return
	 */
	public Resp getOneKnowledge(JSONObject jo) {
		if (containsKey(jo, "knowledgeId")) {
			Record knowledge = Knowledge.dao.findDetailById(jo.getString("knowledgeId"));
			if(null != knowledge) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("查询成功");
				resp.setData(knowledge);
			}else {
				resp.setCode(CodeConst.KNOWLEDGE_NOT_FOUND);
				resp.setMsg("查看的知识未找到");
			}
		}else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}

	public Resp getKnowledgeTypes(JSONObject jo) {
		List<Record> list = Knowledge.dao.findAllKnowledgeType();
		if(null != list) {
			resp.setCode(CodeConst.SUCCESS);
			resp.setMsg("查询成功");
			resp.setData(list);
		}else {
			resp.setCode(CodeConst.WARN);
			resp.setMsg("查询结果为空");
		}
		return resp;
	}
	
	

}
